﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            this.xuatluoi();
       
    }
    void xuatluoi()
    {
        //var bang = (ShoppingCart)Session["ASPNETShoppingCart"];
        GVgiohang.DataSource = ShoppingCart.Instance.Items;
        //GVgiohang.DataKeyNames={"masanpham"};
        GVgiohang.DataBind();
        if (ShoppingCart.Instance.Items.Count > 0)
        {
            Label lb_mathang = (Label)GVgiohang.FooterRow.FindControl("lb_mathang");
            lb_mathang.Text = ShoppingCart.Instance.Items.Count.ToString();
            Label lb_tongtien = (Label)GVgiohang.FooterRow.FindControl("tongtien");
            lb_tongtien.Text = ShoppingCart.Instance.GetSubTotal().ToString("#,##0 VND");
        }
        
    }



   
    protected void GVgiohang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "xoa")
        {
            
            int masp = Convert.ToInt32(e.CommandArgument);
            ShoppingCart.Instance.RemoveItem(masp);
            xuatluoi();
        }
    }
    protected void bt_capnhat_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow row in GVgiohang.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {

                try
                {
                    // Lấy productId
                    int productId = Convert.ToInt32(GVgiohang.DataKeys[row.RowIndex].Value);
                    // Tìm và lấy giá trị của số lượng
                    int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("So_luong")).Text);
                    ShoppingCart.Instance.SetItemQuantity(productId, quantity);
                }
                catch (FormatException) { }
            }
        }
        xuatluoi();
    }
    protected void GVgiohang_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
}
